﻿using System;
using MoonSharp.Interpreter;
using UnityEngine;

// Token: 0x02000203 RID: 515
public class LuaMotiveInstance : IMotiveInstance
{
	// Token: 0x06000AF1 RID: 2801 RVA: 0x0005CCA4 File Offset: 0x0005AEA4
	public LuaMotiveInstance(Table i, Script s)
	{
		this.instance = i;
		this.script = s;
		this.start = this.GetMethod("Start");
		this.update = this.GetMethod("Update");
		this.getValue = this.GetMethod("GetValue");
	}

	// Token: 0x06000AF2 RID: 2802 RVA: 0x0005CCF8 File Offset: 0x0005AEF8
	private DynValue GetMethod(string methodName)
	{
		DynValue dynValue = this.instance.Get(methodName);
		if (dynValue.IsNil())
		{
			dynValue = this.instance.MetaTable.Get(methodName);
			if (dynValue.IsNil())
			{
				dynValue = this.instance.MetaTable.Get("__index").Table.MetaTable.Get(methodName);
			}
		}
		return dynValue;
	}

	// Token: 0x06000AF3 RID: 2803 RVA: 0x0005CD61 File Offset: 0x0005AF61
	public void Start()
	{
		this.RunMethod(this.start);
	}

	// Token: 0x06000AF4 RID: 2804 RVA: 0x0005CD70 File Offset: 0x0005AF70
	public void Update()
	{
		this.RunMethod(this.update);
	}

	// Token: 0x06000AF5 RID: 2805 RVA: 0x0005CD80 File Offset: 0x0005AF80
	public float GetValue()
	{
		DynValue dynValue = this.RunMethod(this.getValue);
		if (dynValue != null && dynValue.Type == DataType.Number)
		{
			float num = (float)dynValue.Number;
			if (num < 0f)
			{
				num = 0f;
				Debug.LogError(this.behaviorFilename + ": GetValue() must return a value between 0 and 1.");
			}
			else if (num > 1f)
			{
				num = 1f;
				Debug.LogError(this.behaviorFilename + ": GetValue() must return a value between 0 and 1.");
			}
			return num;
		}
		Debug.LogError(this.behaviorFilename + ": GetValue() must return a value between 0 and 1.");
		return 0f;
	}

	// Token: 0x06000AF6 RID: 2806 RVA: 0x0005CE24 File Offset: 0x0005B024
	private DynValue RunMethod(DynValue method)
	{
		if (method.IsNil())
		{
			return null;
		}
		DynValue result;
		try
		{
			result = this.script.Call(method, new object[]
			{
				this.instance
			});
		}
		catch (ScriptRuntimeException ex)
		{
			Debug.LogError(ex.DecoratedMessage);
			result = null;
		}
		return result;
	}

	// Token: 0x04001007 RID: 4103
	public string behaviorFilename;

	// Token: 0x04001008 RID: 4104
	private Table instance;

	// Token: 0x04001009 RID: 4105
	public Script script;

	// Token: 0x0400100A RID: 4106
	private DynValue start;

	// Token: 0x0400100B RID: 4107
	private DynValue update;

	// Token: 0x0400100C RID: 4108
	private DynValue getValue;
}
